var SPSdk = (function () {
    var SDK = "http://h5.hortorgames.com/sdk/sdk_agent.min.js";
    var useSDK;
    function SPSdk() {
        this.spId = 0;
        this.appId = null;
        this.appKey = "";
        this.token = "";
        this.spFname = "";
        this.openKey = "";
        this.queryParams = null;
        this.paysuccess = null;
        this.configData = null;
    }

    var __proto = SPSdk.prototype;

    __proto.login = function (param, callback) {
        var m_this = this;
        console.log("疯狂爱游戏登录：" + JSON.stringify(param));
        callback && callback({"result":0,"spuid":m_this.queryParams.userId,"isSubscribe":m_this.queryParams.isSubscribe,"nickname":decodeURIComponent(m_this.queryParams.userName)});
    }

    __proto.showQRCode = function(param, callback){
        var m_this = this;
        if(m_this.queryParams.isSubscribe = true)
        {
            callback&&callback(JSON.stringify({"result":0,"desc":"OK"}));
        }
        else
            useSDK.showQRCode();
        return true;
    }

    __proto.pay = function (param, callback) {
        var m_this = this;
        console.log("疯狂爱游戏支付：" + JSON.stringify(param));
        var other = param.data.other;
        m_this.paysuccess = callback;
        useSDK && useSDK.pay({"order_id":other.order_id,"app_id":other.app_id,"timestamp": other.timestamp, "nonce_str": other.nonce_str, "package":other.package, "sign_type": other.sign_type, "pay_sign": other.pay_sign});
    };
    __proto.config = function (param) {
        var m_this = this;
        m_this.configData = param;
        m_this.myConfig();
    }
    __proto.myConfig = function() {
        console.log("疯狂爱游戏配置：" + JSON.stringify(param));
        var m_this = this;
        if(m_this.configData && m_this.appId)
        {
            useSDK && useSDK.config({
                gameId: m_this.appId,
                share:{
                    timeline:{title:m_this.configData.title,imgUrl:m_this.configData.imgsrc},
                    friend:{title:m_this.configData.title,desc:m_this.configData.desc,imgUrl:m_this.configData.imgsrc}
                },
                pay:{
                    success:function(){
                        m_this.paysuccess&&m_this.paysuccess();
                    },
                    cancel:function(){
                        m_this.paysuccess&&m_this.paysuccess();
                    }
                }
            });
        }
    }

    __proto.init = function (param, callback) {
        console.log("疯狂爱游戏初始化：" + JSON.stringify(param));
        var m_this = this;
        LayaCommon.loadScript(SDK, function ()
        {
            useSDK = window.HORTOR_AGENT;
            useSDK.init();
            m_this.myConfig();
            m_this.access_token = param.access_token;
            m_this.spId = param.spId;
            m_this.appId = param.appId;
            m_this.spFname = param.spFname;
            m_this.openKey = param.openKey;
            m_this.queryParams = param.queryParams;
            callback && callback({"result": 0, "desc": "OK"});
        });
    };
    return SPSdk;
})();